<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改通知公告')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: summernote-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-gonggao-edit" th:object="${gonggao}">
            <input name="gonggaoid" th:field="*{gonggaoid}" type="hidden">
            <div class="form-group">    
                <label class="col-sm-3 control-label">公告标题</label>
                <div class="col-sm-8">
                    <input name="gonggaobiaoti" th:field="*{gonggaobiaoti}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required" >公告类型</label>
                <div class="col-sm-8">
                    <select name="gonggaoleixing" class="form-control m-b" th:with="type=${@dict.getType('sys_gonggao_type')}">
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:field="*{gonggaoleixing}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">公告内容</label>
                <div class="col-sm-8">
                    <input id="gonggaoneirong" name="gonggaoneirong" th:field="*{gonggaoneirong}" type="hidden">
                    <div id="editor" class="summernote"></div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">公告状态</label>
                <div class="col-sm-8">
                    <select name="gonggaozhuangtai" class="form-control m-b" th:with="type=${@dict.getType('sys_gonggao_status')}">
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:field="*{gonggaozhuangtai}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">是否置顶</label>
                <div class="col-sm-8">
                    <select name="beizhu1" class="form-control" th:field="*{beizhu1}">
                        <option value="1">置顶</option>
                        <option value="0">不置顶</option>
                    </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">接收人</label>
                <div class="col-sm-8">
                    <div class="input-group">
                        <input name="jieshourennames" onclick="selectJieshourenTree()" id="jieshourennames" type="text"
                               placeholder="请选择接收人" class="form-control" th:field="*{jieshouren}">
                        <input name="jieshourenid" id="jieshourenids" class="form-control" style="display:none" th:field="*{jieshourenid}">
                        <span class="input-group-addon"><i class="fa fa-search"></i></span>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">文件上传</label>
                <div class="col-sm-8">
                    <input id="filePath" name="fujianurl" class="form-control" type="file">
                    <a id="fujianurls" th:href="@{*{fujianurl}}" th:text="*{fujianurl}"  target="_blank"></a>
                </div>
            </div>
            <div class="form-group" style="display: none">
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input name="fabushijian" th:value="${#dates.format(gonggao.fabushijian, 'yyyy-MM-dd HH:mm:ss')}" class="form-control" type="text">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <th:block th:include="include :: summernote-js" />
    <script th:inline="javascript">
        var prefix = ctx + "system/gonggao";

/*        //显示已上传文件
        $("#filePath").on("change",function(e){
            var e=e||window.event
            var _file=e.target.files
            var names=_file[0].name
/!*            if(_file.length>0){
                for(let i=0;i<_file.length;i++){
                    if(i==_file.length-1){
                        names+=_file[i].name
                    }else{
                        names+=_file[i].name+"<br>"
                    }
                }*!/
            $("#fujianurls").innerText(names)
/!*            }else{
                $("#fujianurls").html("")
            }*!/
        })*/

        /* 通知公告-新增-选择接收人 */
        function selectJieshourenTree() {
            $.modal.open("选择接收人", "/system/gonggao/JieshourenTree", 1400 ,800,doSubmit);
        }

        function doSubmit(index, layero) {
            var iframeWin = $(layero).find("iframe")[0].contentWindow
            var rows = iframeWin.getSelections();
            var jieshourenids=new Array();
            var jieshourennames=new Array();
            if(rows.length>0){
                for (var i = 0; i < rows.length; i++) {
                    jieshourenids.push(rows[i]["userId"]);
                    jieshourennames.push(rows[i]["userName"]);
                }
                $("#jieshourenids").val(jieshourenids);
                $("#jieshourennames").val(jieshourennames);
            }else {
                $("#jieshourenids").val('0');
                $("#jieshourennames").val('所有人');
            }
            $.modal.close(index);

        }

        $(function() {
            $('.summernote').summernote({
                placeholder: '请输入公告内容',
                height : 192,
                lang : 'zh-CN',
                followingToolbar: false,
                dialogsInBody: true,
                callbacks: {
                    onImageUpload: function (files) {
                        sendFile(files[0], this);
                    }
                }
            });
            var content = $("#gonggaoneirong").val();
            $('#editor').summernote('code', content);

        });

        // 公告内容中加图片
        function sendFile(file, obj) {
            var data = new FormData();
            data.append("file", file);
            $.ajax({
                type: "POST",
                url: ctx + "common/upload",
                data: data,
                cache: false,
                contentType: false,
                processData: false,
                dataType: 'json',
                success: function(result) {
                    if (result.code == web_status.SUCCESS) {
                        $(obj).summernote('editor.insertImage', result.url, result.fileName);
                    } else {
                        $.modal.alertError(result.msg);
                    }
                },
                error: function(error) {
                    $.modal.alertWarning("图片上传失败。");
                }
            });
        }



        //文件上传
        function uploadFile() {
            var formData = new FormData();
            formData.append('file', $('#filePath')[0].files[0]);

            $.ajax({
                url: ctx + "common/upload",
                type: 'post',
                cache: false,
                data: formData,
                processData: false,
                contentType: false,
                dataType: "json",
                async: false,
                success: function(result) {
                    if (result.code == web_status.SUCCESS) {
                        var fujianurl = result.url
                        $('#filePath').attr("fujianurl",fujianurl)
                    } else {
                        $.modal.alertError(result.msg);
                    }
                }
            });
        }

        $("#form-gonggao-edit").validate({
            focusCleanup: true
        });


        function submitHandler() {
            if ($.validate.form()) {
                var sHTML = $('.summernote').summernote('code');
                $("#gonggaoneirong").val(sHTML);

                //有上传文件添加fujianurl
                var formdata = $('#form-gonggao-edit').serialize()
                if ($('#filePath')[0].files[0] != null) {
                    uploadFile()
                    formdata += "&fujianurl=" + $('#filePath').attr('fujianurl');
                }


                $.operate.save(prefix + "/edit", formdata);
            }
        }

    </script>
</body>
</html>